import cherrypy
from web_ui import *

'''
Reference: http://tools.cherrypy.org/wiki/AuthenticationAndAccessRestrictions 
'''
def login_screen(from_page='/', username='', error_msg='' ):
    template = template_ui()
    '''
     -->> get_parmas = urllib.quote(cherrypy.request.request_line.split()[1])
    if conditions is not None:
        username = cherrypy.session.get(SESSION_KEY)
        if username:
            cherrypy.request.login = username
            for condition in conditions:
                # A condition is just a callable that returns true orfalse
                if not condition():
                    # Send old page as from_page parameter
                    raise cherrypy.HTTPRedirect("/auth/login?from_page=%s" % get_parmas)

    
    '''
    
    
    '''
    Login then send requestor to page rather than home screen...
    '''
    #get_parmas = urllib.quote(cherrypy.request.request_line.split()[1])
    
    cherrypy.log(msg='Render Login (%s - %s - %s )' %(from_page, username, error_msg), context='', severity=20, traceback=False)
    return template.render('login.html', {'from_page': from_page, 'err_msg': error_msg})





def loadUserByUsername(login): 
    cherrypy.log(msg='---- Loading user information', context='', severity=20, traceback=False)
    ulist=[("user1","pass1"),("user2","pass2")] 
    for u,p in ulist: 
        if u==login: 
            return (u,p) 
def checkLoginAndPassword(login, password): 
    cherrypy.log(msg='--- Verifying Password (%s, %s) ' % (login, password), context='', severity=20, traceback=False)
    user = loadUserByUsername(login) 
    if user==None: 
        return u'Bad username or password. Please try again' 


